package com.chuanghai.light.service;

import com.chuanghai.light.dao.LightPointDao;
import com.chuanghai.light.dao.LightTimeListDao;
import com.chuanghai.light.entity.DevTime;
import com.chuanghai.light.entity.LightTimeList;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LightTimeListService  {

	@Resource
	private LightTimeListDao lightTimeListDao;
	@Resource
	private LightPointService lightPointService;

	/**
	 * 获取表中指定点位时间信息
	 * @param pointCode
	 * @return
	 */
	public Map<String, Object> getDevTimeByPointCode(String pointCode){
		List<Map<String, Object>> params = new ArrayList<>();
		List<LightTimeList> lightTimeList = lightTimeListDao.findAllByPointCode(pointCode);
		boolean currentState = lightPointService.findStatePointCode(pointCode);

		if (lightTimeList!=null){
			for (LightTimeList lightTime : lightTimeList) {
				System.out.printf(lightTime.getOnTime()+"");
				Map<String, Object> param = new HashMap<>();
				param.put("onTime", lightTime.getOnTime());
				param.put("offTime", lightTime.getOffTime());
				params.add(param);
			}
		}
		Map<String, Object> returnParam = new HashMap<>();
		returnParam.put("pointCode", pointCode);
		returnParam.put("currentState", currentState);
		returnParam.put("timeInfo",params);
		return returnParam;
	}


	/**
	 * 设置指定点位的时间列表,，会删除之前的记录
	 * @param devTime
	 */
	public void setDevTimeBypointCode(DevTime devTime)
	{
		System.out.printf(devTime.getPointCode());
		// 先删除之前的
		List<LightTimeList> lightTimeList = lightTimeListDao.findAllByPointCode(devTime.getPointCode());
		for(LightTimeList lightTime:lightTimeList) {
			lightTimeListDao.delete(lightTime);
		}

		for(int i=0;i<devTime.getTurnOn().size();i++)
		{
			LightTimeList lightTime = new LightTimeList();
			lightTime.setPointCode(devTime.getPointCode());
			lightTime.setOnTime(devTime.getTurnOn().get(i));
			lightTime.setOffTime(devTime.getTurnOff().get(i));
			lightTimeListDao.save(lightTime);
		}
	}
}
